// 绑定登录按钮的逻辑
(function () {
    // 两个输入框
    let username = document.querySelector('.username');
    let password = document.querySelector('.password');
    // 获取登录按钮
    let loginBtn = document.querySelector('.login-btn');
    loginBtn.addEventListener('click', function () {
        // 尝试获取两个输入框的值
        let username_value = username.value;
        let password_value = password.value;
        console.log(username_value, password_value);
        // 接入登录接口
        fetch('http://127.0.0.1:3000/login/login', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                username: username_value,
                password: password_value
            })
        }).then((response) => {
            return response.json()
        }).then((data) => {
            console.log('Success:', data);
            console.log(data.data.token);
            localStorage.setItem('token', data.data.token);
            if (data.success) {
                // 使用 a标签 进行跳转
                let a = document.createElement('a');
                a.href = '/index.html';
                a.click();
            } else {
                alert('用户名或密码错误！');
            }
        }).catch(error => {
            console.error('Error:', error);
            alert('用户名或密码错误！');
        });
    });
})();